package s04;

import base.Bean;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InvocationHandler;

import java.lang.reflect.Method;

/**
 * @author wangpp
 */
public class InvocationHandlerTest {
    public static void main(String[] args) {
        Enhancer e = new Enhancer();
        e.setSuperclass(Bean.class);
        e.setCallback(new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println(method.getDeclaringClass());
                System.out.println(method.getReturnType());

                if (method.getDeclaringClass() != Object.class &&
                        method.getReturnType() == String.class) {
                    return "hello invocationHandler";
                } else {
                    throw new RuntimeException("error invocationHandler");
                }
            }
        });

        Bean o = (Bean) e.create();
        System.out.println(o.test("null"));
        System.out.println(o.toString());
    }
}
